﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/18 2:08:33
 * ************************************************************/
namespace Mozlite.FileStorages
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common;
    using System.IO;

    /// <summary>
    /// 临时文件夹清理任务。
    /// </summary>
    public class TemplateFolderCleanTask : TaskBase
    {
        protected override void SetExecuteTime()
        {
            this.SetIntervalExecuteTime(Globals.HourSeconds * 2);
        }

        protected override void Action()
        {
            var directory = new DirectoryInfo(Globals.GetPath("temp"));
            if (!directory.Exists)
                return;
            List<string> deletes = new List<string>();
            foreach (var info in directory.GetDirectories())
            {
                if (info.LastWriteTime.AddHours(2) < DateTime.Now)
                    deletes.Add(info.FullName);
            }
            foreach (var file in deletes)
                Directory.Delete(file);
            foreach (var info in directory.GetFiles())
            {
                if (info.LastWriteTime.AddHours(2) < DateTime.Now)
                    deletes.Add(info.FullName);
            }
            foreach (var file in deletes)
                File.Delete(file);
        }

        public override bool Enabled
        {
            get { return true; }
        }

        public override ExecuteMode ExecuteMode
        {
            get { return Common.ExecuteMode.Interval; }
        }
    }
}